Passed
Push — 1 ( f8e5cb...ba63b3 )
by Robbie
03:44
created

CampaignActions.js ➔ setNewItem   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
/* global window */
2
import ACTION_TYPES from './CampaignActionTypes';
3
import RECORD_ACTION_TYPES from 'state/records/RecordsActionTypes';
4
5
export function setShowMessage(show, storage = window.localStorage) {
6
  storage.setItem('campaign.showMessage', show);
7
  return {
8
    type: ACTION_TYPES.SET_SHOW_MESSAGE,
9
    payload: { show },
10
  };
11
}
12
13
/**
14
 * Set selected changeset item
15
 *
16
 * @param {number} changeSetItemId ID of changesetitem in the currently visible changeset
17
 * @return {object}
18
 */
19
export function selectChangeSetItem(changeSetItemId) {
20
  return {
21
    type: ACTION_TYPES.SET_CAMPAIGN_SELECTED_CHANGESETITEM,
22
    payload: { changeSetItemId },
23
  };
24
}
25
26
/**
27
 * Show specified campaign set
28
 *
29
 * @param {number} campaignId ID of the Campaign to show.
30
 * @param {string} view The view mode to display the Campaign in.
31
 * @return {function}
32
 */
33
export function showCampaignView(campaignId, view) {
34
  return (dispatch) => {
35
    dispatch({
36
      type: ACTION_TYPES.SET_CAMPAIGN_ACTIVE_CHANGESET,
37
      payload: { campaignId, view },
38
    });
39
  };
40
}
41
42
/**
43
 * Publish a campaign and all its items
44
 *
45
 * @param {Function} publishApi See lib/Backend
46
 * @param {string} recordType
47
 * @param {number} campaignId
48
 * @return {Object}
49
 */
50
export function publishCampaign(publishApi, recordType, campaignId) {
51
  return (dispatch) => {
52
    dispatch({
53
      type: ACTION_TYPES.PUBLISH_CAMPAIGN_REQUEST,
54
      payload: { campaignId },
55
    });
56
57
    publishApi({ id: campaignId })
58
      .then((data) => {
59
        dispatch({
60
          type: ACTION_TYPES.PUBLISH_CAMPAIGN_SUCCESS,
61
          payload: { campaignId },
62
        });
63
        dispatch({
64
          type: RECORD_ACTION_TYPES.FETCH_RECORD_SUCCESS,
65
          payload: { recordType, data },
66
        });
67
      })
68
      .catch((error) => {
69
        dispatch({
70
          type: ACTION_TYPES.PUBLISH_CAMPAIGN_FAILURE,
71
          payload: { error },
72
        });
73
      });
74
  };
75
}
76
77
/**
78
 * Set new campaign
79
 *
80
 * @param {Number|null} itemId
81
 * @return {Object}
82
 */
83
export function setNewItem(itemId) {
84
  return {
85
    type: ACTION_TYPES.SET_NEW_CAMPAIGN,
86
    payload: { newItem: itemId },
87
  };
88
}
89
90
/**
91
 * Delete a campaign item
92
 *
93
 * @param {Function} removeCampaignItemApi See lib/Backend
0 ignored issues
show
Documentation introduced by
The parameter removeCampaignItemApi does not exist. Did you maybe forget to remove this comment?
Loading history...
94
 * @param {string} recordType
0 ignored issues
show
Documentation introduced by
The parameter recordType does not exist. Did you maybe forget to remove this comment?
Loading history...
95
 * @param {number} campaignId
96
 * @return {Object}
97
 */
98
export function removeCampaignItem(removeItemApi, campaignId, itemId) {
99
  return (dispatch) => {
100
    dispatch({
101
      type: ACTION_TYPES.REMOVE_CAMPAIGN_ITEM_REQUEST,
102
      payload: { campaignId, itemId },
103
    });
104
105
    return removeItemApi({ id: campaignId, itemId })
106
      .then(() => {
107
        dispatch({
108
          type: ACTION_TYPES.REMOVE_CAMPAIGN_ITEM_SUCCESS,
109
          payload: { campaignId, itemId },
110
        });
111
      })
112
      .catch((error) => {
113
        dispatch({
114
          type: ACTION_TYPES.REMOVE_CAMPAIGN_ITEM_FAILURE,
115
          payload: { error },
116
        });
117
      });
118
  };
119
}
120